﻿using System;
using System.Collections.Generic;
using System.Web.UI;
using DevExpress.Web.ASPxHeadline;
using DevExpress.Web.ASPxClasses;
using DevExpress.Web.ASPxCallback;

using LMD.Constant;
using LMD.Lib.DataClass;
using LMD.Lib.Interface;

public partial class CDNew : ICPRelationshipPage
{
    public CDData m_cdData;
    #region Properties
    public int m_iCID
    {
        get
        {
            if (!String.IsNullOrEmpty(Request.QueryString["cid"]))
            {
                return Convert.ToInt32(Request.QueryString["cid"]);
            }
            else
            {
                return -1;
            }
        }
    }
	public int m_iCDID
    {
        get
        {
            if (!String.IsNullOrEmpty(Request.QueryString["cdid"]))
            {
                return Convert.ToInt32(Request.QueryString["cdid"]);
            }
            else
            {
                return -1;
            }
        }
    }
    #endregion Properties

    #region Event Handlers

    protected override void Page_Load(object sender, EventArgs e)
    {
        m_lsPermittedRoles.Add(ICPUserRole.CAR);
        base.Page_Load(sender, e);

        Page.Title = "ICP Lintasarta: Relationship, Eval, Config & Datek - Upload New";
        if (!IsPostBack && !IsCallback)
        {
            
        }
    }
    protected void cbkCDNew_Callback(object sender, CallbackEventArgs e)
    {
        System.Threading.Thread.Sleep(KGlobal.AJAX_WAIT_TIME);
        if(e.Parameter.ToUpper().StartsWith("LOAD"))
        {
            e.Result = HandleLoad(e.Parameter);
        }
        else if (e.Parameter.ToUpper().StartsWith("SAVE"))
        {
            e.Result = HandleSave(e.Parameter);
        }
        else if (e.Parameter.ToUpper().StartsWith("CANCEL"))
        {
            e.Result = HandleCancel();
        }
        else if (e.Parameter.ToUpper().StartsWith("EDIT"))
        {
            e.Result = HandleEdit(e.Parameter);
        }
    }
    #endregion Event Handlers

    #region Custom Methods
    protected override void DoSearch(String sxSearchQuery)
    {
        throw new NotImplementedException();
    }
    public String InitCDData(int iCustomerID, int iCDID)
    {
        CDData cdData = new CDData();
        Cached.KonfigurasiDatekTable cachedKonfigDatek = new Cached.KonfigurasiDatekTable();
        cdData = cachedKonfigDatek.LoadCDDataByCustomerId(iCustomerID, iCDID, true);

        return CDManager.BuildFieldSet_UpdateCDData(cdData);
    }
    private String HandleLoad(String sxParameter)
    {
        String[] sxSplitted = sxParameter.Split(new String[] { KGlobal.DELIMITER }, StringSplitOptions.None);

        String sxReturn = String.Empty;
        int iCustomerID = Convert.ToInt32(sxSplitted[1]);
        String sxCompanyName = DBQuery.CustomerTable.GetCompanyNameByID(iCustomerID);
        m_cdData = new CDData();
        //Cached.KonfigurasiDatekTable cachedKonfigDatek = new Cached.KonfigurasiDatekTable();

        //m_cdData = cachedKonfigDatek.LoadCDDataByCustomerId(iCustomerID, true);
        //m_cdData = new CDData();
        //if (m_cdData.iCustomerID > 0)//(m_cdData != null)
        //{
            //sxReturn = "LOAD" + KGlobal.DELIMITER + CDManager.BuildFieldSet_UpdateCDData(m_cdData);
            sxReturn = "LOAD" + KGlobal.DELIMITER + m_cdData.BuildEditor_CDData(sxCompanyName, iCustomerID);
        //}
        //else
        //{
        //    sxReturn = "LOAD" + KGlobal.DELIMITER + m_cdData.BuildEditor_CDData(sxCompanyName, iCustomerID);
        //}
        return sxReturn;
    }
    private String HandleSave(String sxParameter)
    {
        CDData cdData = new CDData();
        String[] sxSplitted = sxParameter.Split(new String[] { KGlobal.DELIMITER }, StringSplitOptions.None);
        foreach (String sxRow in sxSplitted)
        {
            String[] sxRowSplitted = sxRow.Split(new String[] { KGlobal.NEWLINE }, StringSplitOptions.None);

            switch (sxRowSplitted[0].ToUpper())
            {
                case KCD.CUSTOMER_ID:
                    cdData.iCustomerID = Convert.ToInt32(sxRowSplitted[1]);
                    break;
                case KCD.DATEK_FILE_ID:
                    if(!String.IsNullOrEmpty(sxRowSplitted[1]))
                    {
                        cdData.iDatekFileID = Convert.ToInt32(sxRowSplitted[1]);
                    }
                    else
                    {
                        cdData.iDatekFileID = -1;
                    }
                    break;
                case KCD.EVALUASI_FILE_ID:
                    if (!String.IsNullOrEmpty(sxRowSplitted[1]))
                    {
                        cdData.iEvaluasiFileID = Convert.ToInt32(sxRowSplitted[1]);
                    }
                    else
                    {
                        cdData.iEvaluasiFileID = -1;
                    }
                    break;
                case KCD.CONFIG_FILE_ID:
                    if (!String.IsNullOrEmpty(sxRowSplitted[1]))
                    {
                        cdData.iConfigFileID = Convert.ToInt32(sxRowSplitted[1]);
                    }
                    else
                    {
                        cdData.iConfigFileID = -1;
                    } break;
                case KCD.NOTE:
                    cdData.sxNote = sxRowSplitted[1].ToString();
                    break;
                case KCD.INFO:
                    cdData.sxInfo = sxRowSplitted[1].ToString();
                    break;
            }
        }
        DBQuery.KonfigurasiDatekTable.SaveCDData(cdData);
		Cached.KonfigurasiDatekTable ccCD = new Cached.KonfigurasiDatekTable();
        ccCD.RemoveCache();
        return "SAVE" + KGlobal.DELIMITER + CDManager.BuildFieldSet_UpdateCDData(cdData);
    }
    private String HandleEdit(String sxParameter)
    {
        CDData cdData = new CDData();
        String[] sxSplitted = sxParameter.Split(new String[] { KGlobal.DELIMITER }, StringSplitOptions.None);
        foreach (String sxRow in sxSplitted)
        {
            String[] sxRowSplitted = sxRow.Split(new String[] { KGlobal.NEWLINE }, StringSplitOptions.None);

            switch (sxRowSplitted[0].ToUpper())
            {
                case KCD.CDID:
                    cdData.iCDID = Convert.ToInt32(sxRowSplitted[1]);
                    break;
                case KCD.CUSTOMER_ID:
                    cdData.iCustomerID = Convert.ToInt32(sxRowSplitted[1]);
                    break;
                case KCD.DATEK_FILE_ID:
                    if (!String.IsNullOrEmpty(sxRowSplitted[1]))
                    {
                        cdData.iDatekFileID = Convert.ToInt32(sxRowSplitted[1]);
                    }
                    else
                    {
                        cdData.iDatekFileID = -1;
                    }
                    break;
                case KCD.EVALUASI_FILE_ID:
                    if (!String.IsNullOrEmpty(sxRowSplitted[1]))
                    {
                        cdData.iEvaluasiFileID = Convert.ToInt32(sxRowSplitted[1]);
                    }
                    else
                    {
                        cdData.iEvaluasiFileID = -1;
                    }
                    break;
                case KCD.CONFIG_FILE_ID:
                    if (!String.IsNullOrEmpty(sxRowSplitted[1]))
                    {
                        cdData.iConfigFileID = Convert.ToInt32(sxRowSplitted[1]);
                    }
                    else
                    {
                        cdData.iConfigFileID = -1;
                    } break;
                case KCD.NOTE:
                    cdData.sxNote = sxRowSplitted[1].ToString();
                    break;
                case KCD.INFO:
                    cdData.sxInfo = sxRowSplitted[1].ToString();
                    break;
            }
        }
        DBQuery.KonfigurasiDatekTable.EditCDData(cdData);
		Cached.KonfigurasiDatekTable ccCD = new Cached.KonfigurasiDatekTable();
        ccCD.RemoveCache();
        return "EDIT" + KGlobal.DELIMITER + CDManager.BuildFieldSet_UpdateCDData(cdData);
    }
    private String HandleCancel()
    {
        return "CANCEL" + KGlobal.DELIMITER + CDManager.BuildEditor_CompanyList();
    }
    #endregion Custom Methods
}
